package com.bj.zspace.cat.controller;

import com.dianping.cat.Cat;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.Heartbeat;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @Author:liuwenqing
 * @Date:2024/12/13 14:44
 * @Description:
 **/
@RestController
public class ProblemTest {

    @GetMapping(value = "/problem/transaction")
    public String transaction() {
        Transaction t = Cat.newTransaction("transanction_error", "t_e");
        try {
            TimeUnit.SECONDS.sleep(1);
            int a = 1/0;
        } catch (Exception e) {
            t.setStatus(e);
        } finally {
            t.complete();
        }
        return "over";
    }

    @GetMapping(value = "/problem/event")
    public String event() {
        Event event = Cat.newEvent("event_error", "e_e");
        try {
            TimeUnit.SECONDS.sleep(1);
            int a = 1/0;
            event.setStatus(Message.SUCCESS);
        } catch (Exception e) {
           event.setStatus(e);
        } finally {
            event.complete();
        }
        return "over";
    }

    @GetMapping(value = "/problem/exception")
    public String exception() {
        Event event = Cat.newEvent("Exception", "e_e");
        try {
            TimeUnit.SECONDS.sleep(1);
            int a = 1/0;
            event.setStatus(Message.SUCCESS);
        } catch (Exception e) {
            event.setStatus(e);
        } finally {
            event.complete();
        }
        return "over";
    }

    @GetMapping(value = "/problem/heartbeat")
    public String heartbeat() {
        Heartbeat heartbeat = Cat.newHeartbeat("heartbeat_error", "h_e");
        try {
            TimeUnit.SECONDS.sleep(1);
            int a = 1/0;
            heartbeat.setStatus(Message.SUCCESS);
        } catch (Exception e) {
            heartbeat.setStatus(e);
        } finally {
            heartbeat.complete();
        }
        return "over";
    }

    @GetMapping(value = "/problem/longcall")
    public String longCall() {
        Transaction t = Cat.newTransaction("PigeonCall", "/hello");
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (Exception e) {
            t.setStatus(e);
        } finally {
            t.complete();
        }
        return "over";
    }

}
